package guoanmaker.provider.business.controller;


import java.sql.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import javax.transaction.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.provider.business.model.ProvideRoom;
import guoanmaker.provider.business.service.ProvideRoomServiceInterface;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.ROOM_URL)
public class ProvideRoomController {
	private final AjaxJson ajaxJson;
	
	@Autowired
	private ProvideRoomServiceInterface roomService;
	
	@Autowired
	public ProvideRoomController(AjaxJson ajaxJson,ProvideRoomServiceInterface roomService){
		this.ajaxJson=ajaxJson;
		this.roomService=roomService;
	}
	
	@ApiOperation(value="添加空间接口", notes="添加空间信息包括新增和修改")
	@PostMapping("/addProvideRoom")
	@Transactional
	public  AjaxJson addProvideRoom(@RequestBody ProvideRoom room){
		try {
			room.setRoomTime(new Date(0));
			room=roomService.addProvideRoom(room);
		} catch (Exception e) {
			ajaxJson.setSuccess(true).setStatus(250).setData("插入失败");
			return ajaxJson;
		}
		try {
			createRoomCodes(room);
		} catch (Exception e) {
			e.printStackTrace();
			roomService.deleteRoom(room.getId());
			ajaxJson.setSuccess(true).setStatus(250).setData("插入失败");
			return ajaxJson;
		}
		ajaxJson.setSuccess(true).setStatus(200).setData(room);
		return ajaxJson;
	} 
	
	@ApiOperation(value="在空间管理中根据空间的名称的关键字、空间的状态以及服务商的id查询匹配的空间", notes="在空间管理中根据空间的名称的关键字、空间的状态以及服务商的id查询匹配的空间")
	@PostMapping("/selectRoomKeyNameBusinessIdStauts")
	//@Transactional
	public  AjaxJson selectRoomKeyNameBusinessIdStauts(String businessId,String keyName,Integer roomState,Integer page, Integer size){
		try {
			Map<String,Object> map=roomService.selectRoomKeyNameBusinessIdStauts(businessId,keyName,roomState,page,size);
			ajaxJson.setSuccess(true).setStatus(200).setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}
	
	@ApiOperation(value="删除空间", notes="删除空间")
	@GetMapping("/deleteRoomId")
	@Transactional
	public  AjaxJson deleteRoomId(String id ){
		try {
			roomService.deleteRoomId(id);
			ajaxJson.setSuccess(true).setStatus(200).setData("删除成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(true).setStatus(200).setData("删除失败");
		}
		return ajaxJson;
	} 
	
	@ApiOperation(value="关闭与打开空间", notes="关闭与打开空间")
	@PostMapping("/closeOpenRoom")
	@Transactional
	public  AjaxJson closeOpenRoom(String id ,Integer roomState){
		try {
			roomService.closeOpenRoom(id, roomState);
			ajaxJson.setSuccess(true).setStatus(200).setData("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("操作失败");
		}
		return ajaxJson;
	} 
	@ApiOperation(value="根据状态查询所有的空间", notes="根据状态查询所有的空间")
	@RequestMapping(value = "/selectRoomListStauts", method = RequestMethod.POST)
	//@Transactional
	public  AjaxJson selectRoomListStauts(Integer roomState,String businessId){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setData(roomService.selectRoomListStauts(roomState,businessId));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("操作失败");
		}
		return ajaxJson;
	} 
	@ApiOperation(value="根据空间ID查询空间详情", notes="根据空间ID查询空间详情")
	@RequestMapping(value = "/selectRoomById", method = RequestMethod.POST)
	//@Transactional
	public  AjaxJson selectRoomById(String id){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setData(roomService.selectRoomById(id));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("操作失败");
		}
		return ajaxJson;
	} 
	@ApiOperation(value="根据空间ID修改空间", notes="根据空间ID修改空间")
	@PostMapping("/updateRoomById")
	@Transactional
	public  AjaxJson updateRoomById(@RequestBody ProvideRoom provideRoom){
		try {
			roomService.updateRoomById(provideRoom);
			ajaxJson.setSuccess(true).setStatus(200).setData("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("修改失败");
		}
		return ajaxJson;
	} 
	public void createRoomCodes(ProvideRoom room){
		roomService.createRoomCodes(room);
		 return;
	}
}
